<?php
class Model_Album extends Model{

    public function getUserAlbum($userid){

        $sql="select t.albumid id,t.caption 
             from album t
             left join site_user s on s.site_userid=t.userid
             where t.isdeleted<>'1'
             and s.site_userid='$userid'
             order by t.createdon desc";

        $data=DB::query(Database::SELECT,$sql)->execute()->as_array();

        if(empty($data)){

            list($insert_id, $total_rows)= DB::insert('album')

                ->columns(array('userid','caption','isdisabled','createdon'))
                    
                ->values(array($userid,'默认专辑','0',date("Y-m-d h:i:sa")))->execute();
            $data[]=array(
                'id'=>$insert_id,
                'caption'=>'默认专辑'
            );

        }

        return $data;

    }
    public  function saveInfo($userid,$id,$caption){

        if($id==""){

            try {

                list($insert_id, $total_rows)= DB::insert('album')

                ->columns(array('userid','caption','isdisabled','createdon'))
                    
                ->values(array($userid,$caption,'0',date("Y-m-d h:i:sa")))->execute();

                Database::instance()->commit();

                return json_encode(array("code"=>"0x00000","id"=>$insert_id));
            }
            catch (Exception $e) {

                Database::instance()->rollback();

                return json_encode(array("code"=>"0x0000x","info"=>"服务器错误，保存失败，请联系管理员或者重试"));
            }
        }
        else{
            try {
                $total_rows=DB::update('post')
                ->set(array('caption'=>$caption))
                ->where('albumid','=',$id)
                ->and_where('userid','=',$userid)
                ->execute();

                Database::instance()->commit();

                return json_encode(array("code"=>"0x00000"));
            }
            catch (Exception $e) {

                Database::instance()->rollback();

                return json_encode(array("code"=>"0x0000x","info"=>"服务器错误，保存失败，请联系管理员或者重试"));
            }
        }

    }
    public function deleteInfo($userid,$id){

        try {
            $sql="update  album set isdeleted='1' where  albumid='$id' and userid='$userid'";
            
             $total_rows=DB::query(null,$sql)->execute();

            Database::instance()->commit();
        
            return json_encode(array("code"=>"0x00000"));

        }

         catch (Exception $e) {

            Database::instance()->rollback();

            return json_encode(array("code"=>"0x0000x","info"=>"服务器错误，保存失败，请联系管理员或者重试"));
        }
    }

    public function getPostInfo($albumid,$offset,$pageSize){

        $sql="select count(1)total
              from post t
              where t.isdeleted<>'1'
              and album='$albumid'";

        $total=DB::query(Database::SELECT,$sql)->execute()->get("total");

        $sql="select t.postid id,t.description,t.images,t.isdisabled,t.view,t.reach,t.review,t.created
             from post t
             where t.isdeleted<>'1'
             and album='$albumid' 
             order by t.created desc limit $offset,$pageSize";

        $data=DB::query(Database::SELECT,$sql)->execute()->as_array();

        return array("total"=>$total,"rows"=>$data);
    }

    public function getAlbumByUser($userid,$offset,$pageSize){

        $sql="select count(1)total
             from album t
             where t.isdeleted<>'1'
             and t.userid='$userid'";

        $total=DB::query(Database::SELECT,$sql)->execute()->get("total");

        $sql="select a.albumid,a.caption,t.postid postid,t.images
             from album a
             left join post t on t.album=a.albumid and t.isdeleted<>'1' and t.userid='$userid'
             where a.isdeleted<>'1'
             and t.userid='$userid'
             group by a.albumid
             order by a.createdon desc,t.created desc limit $offset,$pageSize";

        $data=DB::query(Database::SELECT,$sql)->execute()->as_array();

        return array("total"=>$total,"rows"=>$data);

    }
}